import roleModel from "../../../models/system/roleModel.js";
import userModel from "../../../models/system/userModel.js";
import {Op, QueryTypes} from "sequelize";
import {Result} from "../../../utils/result.js";


const roleService = {
  async addRole(username, password) {
    return roleModel.create({username, password});
  },

  async delRole(id) {
    return roleModel.destroy({
      where: {
        id,
      }
    });
  },

  async getRole(name) {
    let res = await roleModel.findAll({
      where: {
        name: {
          [Op.like]: `%${name}%`
        }
      }
    });
    return Result.success(res);
  },
  async getRoleByOrg() {
    return await roleModel.sequelize.query(
      ` select * from user  ; `,
      {
        type: QueryTypes.SELECT,
        // model:roleModel,
      })
  },
  async getUserByRole(name) {
    let res = await roleModel.findAll({
      attributes: ['id', 'name'],
      include: [
        {
          model: userModel,
          attributes: ['username', 'id'],
        }
      ],
      where: {
        name,
      }
    })
    return Result.success(res);
  }
}
export default roleService;
